package org.dashboard.client.widget.dailytopten;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Node;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;
import org.dashboard.shared.Props;

import java.util.Arrays;

/**
 */
public class DailyTopTenWidget extends SimplePanel {

    interface Binder extends UiBinder<Widget, DailyTopTenWidget> { }
    private static final Binder binder = GWT.create(Binder.class);

    public DailyTopTenWidget(String width, String height, int refreshInterval) {
        super();

        setWidget(binder.createAndBindUi(this));
        
        final Node node = getElement().getChild(0);
        DOM.setStyleAttribute((Element) node, "width", width);
        DOM.setStyleAttribute((Element) node, "height", height);

        final DailyTopTenServiceAsync instance = (DailyTopTenServiceAsync) GWT.create(DailyTopTenService.class);
        String url = GWT.getModuleBaseURL() + "dailytopten";
        ((ServiceDefTarget) instance).setServiceEntryPoint(url);

        final AsyncCallback<String[][]> statesAsyncCallback = new AsyncCallback<String[][]>() {
            public String[][] oldResult;

            public void onFailure(Throwable caught) {
                Window.alert(caught.getMessage());
            }

            public void onSuccess(String[][] result) {
                if(!Arrays.deepEquals(result, oldResult)) {
                    Node rootNode = node.getChild(0).getChild(1);
                    String[] colNames = Props.instance().getTodayTopTenHeader();
                    StringBuilder sb = new StringBuilder();
                    sb.append("<table><tr>");

                    for (int i =0;i!=colNames.length;i++) {
                        sb.append("<td style=\"font-size: 15px;font-weight: bold;\">"+colNames[i]+"</td>");
                    }
                    sb.append("</tr>");

                    for(int i = 0;i!=result.length;i++){
                        String[] row = result[i];
                        sb.append("<tr>");
                        for(int y=0;y!=row.length;y++){
                            sb.append("<td style=\"font-size: 15px;font-weight: bold;\">"+row[y]+"</td>");
                        }
                        sb.append("</tr>");
                    }
                    sb.append("</table>");

                    ((Element)rootNode).setInnerHTML(sb.toString());
                }
                this.oldResult = result;
            }
        };
        final Timer timer = new Timer() {
            @Override
            public void run() {
                instance.getDailyTopTen(statesAsyncCallback);
            }
        };
        timer.scheduleRepeating(refreshInterval);

        instance.getDailyTopTen(statesAsyncCallback);
    }
}
